package com.tanhua.admin.controller;

import com.tanhua.admin.pojo.Freezing;
import com.tanhua.admin.pojo.MovementsVo;
import com.tanhua.admin.service.ManageService;
import com.tanhua.admin.vo.BaseUserMsgVo;
import com.tanhua.admin.vo.UserBaseDataVo;
import com.tanhua.admin.vo.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName DashBoardController
 * @Description TODO
 * @Author JavaDev
 * @Date 2021/1/16 20:05
 * @Version 1.0
 */
@RestController
@RequestMapping("/manage")
@Slf4j
public class ManageController {

    @Autowired
    private ManageService manageService;

    /**
     * 用户管理 - 用户数据翻页
     *
     * @param page
     * @param pagesize
     * @param id
     * @param nickname
     * @param city
     * @return
     */
    @GetMapping("/users")
    public ResponseEntity userManager(
            @RequestParam("page") String page,
            @RequestParam("pagesize") String pagesize,
            @RequestParam("id") String id,
            @RequestParam("nickname") String nickname,
            @RequestParam("city") String city
    ) {
        PageResult result = manageService.userManager(page, pagesize, id, nickname, city);
        if (result == null) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
        return ResponseEntity.ok(result);
    }

    /**
     * 用户管理-用户基本资料
     *
     * @param userID 根据用户ID查询用户详细信息
     * @return
     */
    @GetMapping("/users/{userID}")
    public ResponseEntity userBaseData(
            @PathVariable String userID) {

        UserBaseDataVo userBaseDataVo = manageService.userBaseData(userID);
        return ResponseEntity.ok(userBaseDataVo);
    }


    /**
     * 用户管理-用户冻结操作
     *
     * @param params userId freezingTime freezingRange reasonsForFreezing frozenRemarks
     * @return
     */
    @PostMapping("/users/freeze")
    public ResponseEntity freeze(
            @RequestBody Map<String, Object> params
    ) {

        try {
            Long userId = Long.valueOf(String.valueOf(params.get("userId")));
            Integer freezingTime = Integer.valueOf(String.valueOf(params.get("freezingTime")));
            Integer freezingRange = Integer.valueOf(String.valueOf(params.get("freezingRange")));
            String reasonsForFreezing = String.valueOf(params.get("reasonsForFreezing"));
            String frozenRemarks = String.valueOf(params.get("frozenRemarks"));

            Freezing freezing = Freezing.builder()
                    .userId(userId)
                    .freezingTime(freezingTime)
                    .freezingRange(freezingRange)
                    .reasonsForFreezing(reasonsForFreezing)
                    .frozenRemarks(frozenRemarks)
                    .build();
            Boolean freeze = manageService.freeze(freezing);
            if (freeze) {
                Map<String, Object> result = new HashMap<>(1);
                result.put("message", "冻结成功");
                return ResponseEntity.ok(result);
            }

        } catch (Exception e) {
            log.error("用户冻结操作出错", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 用户管理-用户解冻操作
     *
     * @param params userId freezingTime freezingRange reasonsForFreezing frozenRemarks
     * @return
     */
    @PostMapping("/users/unfreeze")
    public ResponseEntity unfreeze(
            @RequestBody Map<String, Object> params
    ) {

        try {
            Long userId = Long.valueOf(String.valueOf(params.get("userId")));
            String reasonsForThawing = String.valueOf(params.get("reasonsForThawing"));

            String frozenRemarks = String.valueOf(params.get("frozenRemarks")); //  前端传错了将解封原因传到了这个字段上了
            reasonsForThawing = frozenRemarks;


            Freezing freezing = Freezing.builder()
                    .userId(userId)
                    .reasonsForThawing(reasonsForThawing)
                    .build();
            Boolean freeze = manageService.unfreeze(freezing);
            if (freeze) {
                Map<String, Object> result = new HashMap<>(1);
                result.put("message", "解冻成功");
                return ResponseEntity.ok(result);
            }

        } catch (Exception e) {
            log.error("用户冻结操作出错", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 动态管理-动态分页
     *
     * @return
     */
    @GetMapping("/messages")
    public ResponseEntity auditingMovementsList(
            @RequestParam("page") String pageNum,
            @RequestParam("pagesize") String pageSize,
            @RequestParam(value = "uid", required = false) String uid,
            @RequestParam(value = "state", required = false) String state
    ) {
        PageResult movementsPageResult = manageService.auditingMovementsList(pageNum, pageSize, uid, state);
        return ResponseEntity.ok(movementsPageResult);
    }

    /**
     * 动态管理-动态通过审核
     */
    @PostMapping("/messages/pass")
    public ResponseEntity passAuditMovements(@RequestBody ArrayList<String> publishIds) {

        Long count = manageService.passAuditMovements(publishIds);

        Map<String, Object> result = new HashMap<>(1);
        result.put("message", "审核通过了" + count + "条动态");

        return ResponseEntity.ok(result);

    }

    /**
     * 动态管理-撤销审核通过
     *
     * @param publishIds 批量通过的 动态ID
     * @return 返回生效了多少条操作
     */
    @PostMapping("/messages/revocation")
    public ResponseEntity revocationAuditMovements(@RequestBody ArrayList<String> publishIds) {

        Long count = manageService.revocationAuditMovements(publishIds);

        Map<String, Object> result = new HashMap<>(1);
        result.put("message", "撤销了" + count + "条动态的审核通过");

        return ResponseEntity.ok(result);

    }

    /**
     * 动态管理-审核不通过
     *
     * @param publishIds 批量通过的 动态ID
     * @return 返回生效了多少条操作
     */
    @PostMapping("/messages/reject")
    public ResponseEntity rejectAuditMovements(@RequestBody ArrayList<String> publishIds) {

        Long count = manageService.rejectAuditMovements(publishIds);

        Map<String, Object> result = new HashMap<>(1);
        result.put("message", count + "条动态审核不通过");

        return ResponseEntity.ok(result);

    }


    /**
     * 动态管理 - 动态详情
     *
     * @param publishId
     * @return
     */
    @GetMapping("/messages/{publishId}")
    public ResponseEntity queryMovementsInfo(@PathVariable("publishId") String publishId) {

        MovementsVo movementsVo = manageService.queryMovementsInfo(publishId);

        if (movementsVo != null) {
            return ResponseEntity.ok(movementsVo);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 评论管理 - 评论列表翻页
     *
     * @return
     */
    @GetMapping("/messages/comments")
    public ResponseEntity queryCommentsList(
            @RequestParam(value = "page", required = false) String pageNum,
            @RequestParam(value = "pagesize", required = false) String pageSize,
            @RequestParam(value = "sortProp") String sortProp,
            @RequestParam(value = "sortOrder") String sortOrder,
            @RequestParam(value = "messageID") String messageID
    ) {
        PageResult commentsPageResult = manageService.queryCommentsList(pageNum, pageSize, sortProp, sortOrder,messageID);
        return ResponseEntity.ok(commentsPageResult);
    }


    /**
     * 动态管理-视频记录翻页
     *
     * @return
     */
    @GetMapping("/videos")
    public ResponseEntity auditingVideoList(
            @RequestParam("page") String pageNum,
            @RequestParam("pagesize") String pageSize,
            @RequestParam(value = "uid", required = false) String uid,
            @RequestParam(value = "state", required = false) String state
    ) {
        PageResult videosPageResult = manageService.auditingVideoList(pageNum, pageSize, uid, state);
        return ResponseEntity.ok(videosPageResult);
    }

    /**
     * 动态管理-查看登录日志
     *
     * @return
     */
    @GetMapping("/logs")
    public ResponseEntity queryLoggingList(
            @RequestParam("page") String pageNum,
            @RequestParam("pagesize") String pageSize,
            @RequestParam(value = "uid", required = false) String uid,
            @RequestParam(value = "state", required = false) String state
    ) {
        PageResult loggingPageResult = manageService.queryLoggingListByMongoDB(pageNum, pageSize, uid, state);
        return ResponseEntity.ok(loggingPageResult);
    }


}
